package com.ideaaedi.plugin.fastcoding.ui;

import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.settings.AppSettingsState;
import com.ideaaedi.plugin.fastcoding.util.PropertiesComponentUtil;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 设置类需要 实现 Configurable <br/> 注：设置类需要在plugin.xml中的extensions标签里声明
 *
 * @author JustryDeng
 * @since 2022/1/6 21:20:09
 */
public class FastCodingSettings implements Configurable {
    private JPanel jPanel;
    private JTabbedPane tabbedPane1;
    private JRadioButton containCommentRadioButton;
    private JRadioButton notContainCommentRadioButton;
    private JLabel containCommentQuestion;
    private JPanel mockJson;
    private Project project;
    /** 设置信息 */
    private AppSettingsState appSettingsState;
    
    /**
     * 自动构造  idea会携带当前的project 参数信息
     */
    public FastCodingSettings(Project project) {
        this.project = project;
        appSettingsState = AppSettingsState.getInstance(this.project);
        // MockJson相关UI 初始化
        initMockJsonSetting();
    }
    
    /**
     * MockJson相关UI 初始化
     */
    private void initMockJsonSetting() {
        // 添加联动
        containCommentRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                containCommentRadioButton.setSelected(true);
                notContainCommentRadioButton.setSelected(false);
            }
        });
        notContainCommentRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                notContainCommentRadioButton.setSelected(true);
                containCommentRadioButton.setSelected(false);
            }
        });
        
        // 反显
        reset();
    }
    
    @Override
    @Nls(capitalization = Nls.Capitalization.Title)
    public String getDisplayName() {
        return "Fast Coding Settings";
    }
    
    /**
     * 指定点击设置时，显示的组件
     */
    @Override
    public @Nullable
    JComponent createComponent() {
        return jPanel;
    }
    
    /**
     * 设置是否被修改
     */
    @Override
    public boolean isModified() {
        return isModifiedForMockJson();
    }
    
    /**
     * MockJson的设置是否有改动
     */
    public boolean isModifiedForMockJson() {
        return containCommentRadioButton.isSelected() != appSettingsState.MockJson$containComment;
    }
    
    /**
     * 保存修改后的设置
     */
    @Override
    public void apply() throws ConfigurationException {
        // 保存 mock json 的 设置
        if (containCommentRadioButton.isSelected()) {
            PropertiesComponentUtil.setValue(FastCodingConstant.MockJson.containCommentKey, FastCodingConstant.TRUE);
            appSettingsState.MockJson$containComment = true;
        } else {
            PropertiesComponentUtil.setValue(FastCodingConstant.MockJson.containCommentKey, FastCodingConstant.FALSE);
            appSettingsState.MockJson$containComment = false;
        }
    }
    
    @Override
    public void reset() {
        if (appSettingsState.MockJson$containComment) {
            containCommentRadioButton.setSelected(true);
            notContainCommentRadioButton.setSelected(false);
        } else {
            notContainCommentRadioButton.setSelected(true);
            containCommentRadioButton.setSelected(false);
        }
    }
    
}
